<template>
  <div>
    <el-dialog title="适用门店" class="noselect"
               v-dialogdrag=" { target:'.el-dialog__header',container:'.el-dialog',border:'.el-dialog__wrapper'}"
               :close-on-click-modal="false" :visible.sync="showDialog" :before-close="handleClose">
      <div style="margin: 20px;"></div>
          <!--<div class="text-left">已关联门店:</div>-->
          <div style="height:100px;border:1px solid #ccc;margin-top:10px !important;padding:5px 10px;padding-top:10px;overflow: auto;margin:30px;"
               class="text-left">
            <el-tag v-for="i in tags" style="margin-right:20px;">{{i.label}}</el-tag>

          </div>
      <div class="foot-bar-button">
        <!--<el-button type="primary" @click="submitForm('modelForm')" size="medium">提交</el-button>-->
        <el-button @click="resetForm('modelForm')" type="primary" size="medium">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  //注意路径
  export default {
    props: ['isShow', 'id'],  //参数
    data() {
      return {
        tempalates: [],
        modelForm: {},
        showDialog: false,
        activityId: "",
        isSave: true,
        downCategeryId: [],
        isUpdate: false,
        treeData: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        tags:[]
      };
    },
    watch: {
      isShow: function (newValue, oldValue) {
        console.log(this.id);
        this.showDialog = newValue;
        if (newValue) {
          if (this.id != '') {
            this.activityId = this.id;
            this.isSave = false;
            this.isUpdate = true;
            this.$axios.get("auxiliary/comments/showAllBranchs" +
              "?id=" + this.activityId).then(data => {
                  this.tags=data.content
            });
          }
        }
      }
    },
    created() {
      window.this = this;
    },
    methods: {
      newLabel(newList) {
        if (newList) {
          for (var i = 0; i < newList.length; i++) {
            newList[i].label = newList[i].name;
            newList[i].value = newList[i].id + "";
            newList[i].children = null;
          }
        }
      },
      goBack() {
        this.router.go(-1);
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            console.log(this.tempalates);
            var codes = [];
            for (var i = 0; i < this.tempalates.length; i++) {
              if (this.tempalates[i].isShow) {
                codes.push(this.tempalates[i].code);
              }
            }
            console.log(codes);
            if (codes.length <= 0) {
              this.$message.error("请选择权限");
              return;
            }
            this.modelForm.codes = JSON.stringify(codes);
            if (this.isSave) {
              this.$axios.post("organization/privilegeTemplates/create", this.modelForm).then(data => {
                if (data.status == 200) {
                  this.$message({
                    message: data.message,
                    type: 'success'
                  });
                  this.$emit('addSuccess');
                } else {
                  this.$message.error(data.message);
                }

              })

            } else {
              this.modelForm._method = "PUT";
              this.$axios.put("organization/privilegeTemplates/" + this.activityId + "/update", this.modelForm).then(data => {
                if (data.status == 200) {
                  this.$message({
                    message: data.message,
                    type: 'success'
                  });
                  this.$emit('addSuccess');
                } else {
                  this.$message.error(data.message);
                }
              })
            }
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$emit('close');
        this.showDialog = false;
      },
      handleClose(done) {
        this.$emit('close');
        done();
      },
      isNum(item, key) {
        var r = /^\+?[1-9][0-9]*$/;
        if (!r.test(item[key])) {
          item[key] = "0.00"
        } else {
          item[key] = item[key]
        }
      },
      isDouble(item, key) {
        var isNum = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
        if (!isNum.test(item[key])) {
          item[key] = "0.00"
        } else {
          item[key] = item[key]
        }
      }
    }
  }
</script>
<style scoped>
  .checkbox-group {
    float: left;
    background: #f5f5f5;
    border: 1px solid #d1dbe5;
    padding: 0 5px;
    margin-right: 10px;
    margin-bottom: 10px;
  }

  .checkbox-group .input-group-checkbox {
    /*padding: 9px 10px 0 0;*/
    font-size: 14px;
    font-weight: 400;
    line-height: 1;
    color: #555;
    text-align: center;
    float: left;
    font-size: 14px;
  }

  .checkbox-group .check-lable, .checkbox-group .check-close {
    /*padding-top: 5px;*/
    float: left;
    /*margin-bottom: 5px;*/
    font-size: 14px;
  }


</style>
